package com.cxrj.zhsq.partyMember.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Service;

import com.cxrj.zhsq.partyMember.dao.PartyMemberStudyMapper;
import com.cxrj.zhsq.partyMember.entity.PartyMemberStudy;
import com.cxrj.zhsq.partyMember.service.PartyMemberStudyService;
import com.cxrj.zhsq.utils.HtmlUtil;
import com.cxrj.zhsq.utils.IpUtil;
import com.cxrj.zhsq.utils.UploadController;

@Service
public class PartyMemberStudyServiceImpl implements PartyMemberStudyService {
	@Resource
	private PartyMemberStudyMapper partyMemberStudyMapper;

	@Override
	public Map<String, Object> selectPage(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> resultMap=new HashMap<String, Object>();
		Map<String, Object> sqlMap=new HashMap<String, Object>();
		response.setContentType("text/html;charset=utf-8");
		//数据起始位置
		String start=request.getParameter("offset");
		//数据长度
		String length=request.getParameter("limit");
		//总条数
		String recordsTotal = "0";
		//查询条件
		String kod=request.getParameter("kod");
		String sqlStr = " where 1=1 ";
		
		if(kod != null && !kod.equals("")){
			sqlStr += " and ( study_title like " + '"' + '%' + kod + '%' + '"'+')';
		}
		
		sqlMap.put("condition",sqlStr);
		sqlMap.put("start", Integer.parseInt(start));
		sqlMap.put("length", Integer.parseInt(length));
		recordsTotal=partyMemberStudyMapper.getCount(sqlStr)+"";
		List<Map<String, Object>> noticeList=partyMemberStudyMapper.selectPage(sqlMap);
		
		resultMap.put("total", recordsTotal);
		resultMap.put("recordsFiltered", recordsTotal);
		resultMap.put("rows", noticeList);
		
		return resultMap;
	}

	@Override
	public Map<String, Object> selectById(String studyId, HttpServletRequest request) {
		Map<String, Object> resultMap=new HashMap<>();
		try {
			PartyMemberStudy memberStudy=partyMemberStudyMapper.selectByPrimaryKey(studyId);
			if (memberStudy.getStudyContent() != null && !memberStudy.getStudyContent().equals("")) {
				memberStudy.setStudyContent(HtmlUtil.getAbsSource(memberStudy.getStudyContent(), "http://" + IpUtil.getIpAddr(request) + ":" + request.getServerPort() + "/"));
			}
			resultMap.put("partyMemberStudy", memberStudy);
		} catch (Exception e) {
			resultMap.put("partyMemberStudy", "");
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> insertStudy(PartyMemberStudy partyMemberStudy) {
			Map<String, Object> resultMap=new HashMap<>();
			try {
				partyMemberStudy.setCreateTime(new Date());
				partyMemberStudy.setReleaseState("0");
				partyMemberStudyMapper.insertSelective(partyMemberStudy);
				resultMap.put("result", "1");
			} catch (Exception e) {
				resultMap.put("result", "-1");
				e.printStackTrace();
			}
			return resultMap;
		
	}

	@Override
	public Map<String, Object> updateStudy(PartyMemberStudy partyMemberStudy) {
		Map<String, Object> resultMap=new HashMap<>();
		try {
			partyMemberStudyMapper.updateByPrimaryKeySelective(partyMemberStudy);
			resultMap.put("result", "1");
		} catch (Exception e) {
			resultMap.put("result", "-1");
			e.printStackTrace();
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> updateReleaseState(String studyId, String releaseState) {
		Map<String, Object> resultMap=new HashMap<>();
		PartyMemberStudy memberStudy=new PartyMemberStudy();
		memberStudy.setStudyId(studyId);
		memberStudy.setReleaseState(releaseState);
		memberStudy.setReleaseTime(new Date());
		try {
			partyMemberStudyMapper.updateByPrimaryKeySelective(memberStudy);
			resultMap.put("result", "1");
			
		} catch (Exception e) {
			resultMap.put("result", "-1");
			e.printStackTrace();
		}
		return resultMap;
		
	}

	@Override
	public Map<String, Object> deleteStudy(String studyId, HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> resultMap=new HashMap<>();
		 int a=partyMemberStudyMapper.deleteByPrimaryKey(studyId);
		UploadController.deleteDir(studyId, request, response);
		resultMap.put("result", "-1");
		if (a>0) {
			resultMap.put("result", "1");
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> selectMemberStudy(String dataIndex, String pageCount) {
		Map<String, Object> resultMap=new HashMap<String, Object>();
		Map<String, Object> sqlMap=new HashMap<String, Object>();
		sqlMap.put("start", Integer.parseInt(dataIndex));
		sqlMap.put("length", Integer.parseInt(pageCount));
		resultMap.put("list", partyMemberStudyMapper.selectMemberStudy(sqlMap));
		return resultMap;
	}

}
